
layui.define(['form', 'layedit', 'laydate', 'transfer'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    zmnLayUtil.layFormVerify(form);
    // 提交
    form.on('submit(zmn-form-submit)', function (obj) {
        let content = '<div style="font-family:\'ArialMT\', \'Arial\';color: gray;">请仔细阅读页面【温馨提示内容】，确认已了解复制价格表的功能</div><div>&nbsp;</div>';
        let fromName = $("#organTariffId").find('option:checked').text()
        let toName = $("#newTariffId").find('option:checked').text();
        let scopeServ = $("#servCategId").find('option:checked').text();
        let scopeCateg = $("#categOneId").find('option:checked').text();
        let scope = "";
        if(scopeServ=='全部'){
            scope="复制范围为全部服务分类";
        }else{
            scope = "复制范围为"+scopeServ;
            if(scopeCateg=='全部'||scopeCateg==''){
                scope = scope+"下全部分类";
            }else{
                scope = scope+"-"+scopeCateg;
            }
        }
        content = content+'<div style="">您将把'+toName+'价格表复制到'+fromName+'价格表：'+scope+'</div>';
        let data = obj.field;
        layer.confirm(content,{skin : "my-skin",area: ['500px', '300px'],btn: ['确认并复制', '取消']},function(index){
            layer.close(index);
            $.ajax({
                type: "POST",
                url: "/service/tariff/check.action",
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(data),
                success: function (json) {
                    if (json.status === 200) {
                        if(json.data){
                            layer.confirm('当前条件下被复制的价格表不存在通用产品价格表，复制后将会删除原价格表相应数据，确认执行该操作吗？', function (index) {
                                layer.close(index);
                                submitData(data);
                            });
                        }else{
                            submitData(data);
                        }
                    }else {
                        parent.layer.msg(json.message,{"icon":2});
                    }
                }
            });
        });


        return false
    });

    function submitData(data){
        $.ajax({
            type: "POST",
            url: "/service/tariff/copyTariff.action",
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify(data),
            success: function (json) {
                if (json.status === 200) {
                    parent.layer.msg(json.message,{time: 1000},()=>{
                        location.reload();
                    });
                }else {
                    parent.layer.msg(json.message,{"icon":2});
                }
            }
        });
    }
    //原价格表联动被复制的价格表
    form.on("select(organTariffId)", function (obj) {
        let tariffId = obj.value;
        layui.$("#categOneId").html("<option value=''></option>");
        layui.$("#servCategId").html("<option value=''></option>");
        layui.$("#newTariffId").html("<option value=''></option>");
        form.render("select");
        if (zmnUtil.isNull(obj) || obj.value == 0||obj.value == '') {
            return;
        }
        $.ajax({
            type: "GET",
            url: "/service/tariff/loadTariff?tariffId="+tariffId,
            dataType: "json",
            success: function (res) {
                if(res.statusBool&&res.data){
                    layui.$("#newTariffId").html(loadDataToSelectHtml(res.data,"tariff"));
                }
                form.render("select");
            }
        });
    });

    //被复制价格表联动服务分类
    form.on("select(newTariffId)", function (obj) {
        let tariffId = obj.value;
        layui.$("#categOneId").html("<option value=''></option>");
        layui.$("#servCategId").html("<option value=''></option>");
        form.render("select");
        if (zmnUtil.isNull(obj) || obj.value == 0||obj.value == '') {
            return;
        }
        $.ajax({
            type: "GET",
            url: "/service/tariff/loadSerCategory?tariffId="+tariffId,
            dataType: "json",
            success: function (res) {
                if(res.statusBool&&res.data){
                    layui.$("#servCategId").html(loadDataToSelectHtml(res.data,"servCateg",true));
                    layui.$("#toastMessage").html(res.message);
                }
                form.render("select");
            }
        });
    });

    //服务分类联动
    form.on("select(servCategId)", function (obj) {
        let tariffId = layui.$("#newTariffId").val();
        let servCategId = obj.value;
        layui.$("#categOneId").html("<option value=''></option>");
        if (zmnUtil.isNull(obj) || obj.value == 0||obj.value == '') {
            return;
        }
        $.ajax({
            type: "GET",
            url: "/service/tariff/loadBaseCategory?tariffId="+tariffId+"&servCategId="+servCategId,
            dataType: "json",
            success: function (res) {
                if(res.statusBool&&res.data){
                    layui.$("#categOneId").html(loadDataToSelectHtml(res.data,"categOne",true));
                    layui.$("#toastMessage").html(res.message);
                }
                form.render("select");
            }
        });
    });


    function loadDataToSelectHtml(items,attr,needAll){
        let initHtml = "<option value=''></option>";
        if(needAll){
            initHtml = initHtml+"<option value='1'>全部</option>"
        }
        items.forEach(e=>{
            initHtml = initHtml+"<option value='"+e[attr+"Id"]+"'>"+e[attr+"Name"]+"</option>"
        });
        return initHtml;
    }
    $("#cancleBtn").click(function () {
        zmnLayUtil.closeThisTabsChange('/service/tariff/list.action', parent);
    });
    //对外暴露的接口
    exports('serv_service_tariff_copy', {});
});